package io.renren.modules.lanling.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.lanling.entity.SignUpSubjectEntity;
import io.renren.modules.lanling.service.SchoolService;
import io.renren.modules.lanling.service.SignUpSubjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.lanling.entity.SignUpPaymentLogEntity;
import io.renren.modules.lanling.service.SignUpPaymentLogService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import static io.renren.modules.sys.shiro.ShiroUtils.getUserId;


/**
 * 报名日志表
 *
 * @author LANLING
 * @email lanling@gmail.com
 * @date 2018-10-24 10:40:05
 */
@RestController
@RequestMapping("lanling/signuppaymentlog")
public class SignUpPaymentLogController {
    @Autowired
    private SignUpPaymentLogService signUpPaymentLogService;
    @Autowired
    private SignUpSubjectService signUpSubjectService;
    @Autowired
    private SchoolService schoolService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("lanling:signuppaymentlog:list")
    public R list(@RequestParam Map<String, Object> params) {
        Long userId = getUserId();
        params.put("createUserId", userId);

        PageUtils page = signUpPaymentLogService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("lanling:signuppaymentlog:info")
    public R info(@PathVariable("id") Integer id) {
        SignUpPaymentLogEntity signUpPaymentLog = signUpPaymentLogService.queryObject(id);

        return R.ok().put("signUpPaymentLog", signUpPaymentLog);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("lanling:signuppaymentlog:save")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody SignUpPaymentLogEntity signUpPaymentLog) {
        Long userId = getUserId();
        signUpPaymentLog.setCreateUserId(userId);
        //查找lanling_school中id
        int schoolId = schoolService.querySchoolId(userId);
        signUpPaymentLog.setSchoolId(schoolId);
        signUpPaymentLog.setCreateDate(new Date());
        Integer signUpSubjectId = signUpPaymentLog.getSignUpSubjectId();
        SignUpSubjectEntity signUpSubjectEntity = signUpSubjectService.selectById(signUpSubjectId);
        signUpPaymentLog.setSubjectId(signUpSubjectEntity.getSubjectId());
        signUpPaymentLogService.insert(signUpPaymentLog);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("lanling:signuppaymentlog:update")
    public R update(@RequestBody SignUpPaymentLogEntity signUpPaymentLog) {
        ValidatorUtils.validateEntity(signUpPaymentLog);
        signUpPaymentLog.setLastModiftDate(new Date());
        signUpPaymentLogService.update(signUpPaymentLog);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("lanling:signuppaymentlog:delete")
    public R delete(@RequestBody Integer[] ids) {
        signUpPaymentLogService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/handleInvoice")
    public R handleInvoice(@RequestBody Integer[] ids) {
        signUpPaymentLogService.handleInvoice(Arrays.asList(ids));
        return R.ok();
    }

}
